package com.grantheath.erebus.controller.initializationCommands 
{	
	import com.grantheath.erebus.model.interfaces.IErebusAssetLoaderClassReferenceProxy;
	import com.grantheath.erebus.model.interfaces.IErebusLayersProxy;
	import com.grantheath.erebus.model.interfaces.IErebusAssetLoaderProxy;
	import com.grantheath.erebus.model.interfaces.IErebusPageLoaderProxy;
	import com.grantheath.erebus.model.interfaces.IErebusClassReferenceProxy;
	import com.grantheath.erebus.model.enum.notifications.ErebusInitializationNotifications;
	import com.grantheath.erebus.model.interfaces.IErebusPreferencesProxy;
	import com.grantheath.erebus.model.enum.names.ErebusProxyNames;
	import com.grantheath.erebus.model.interfaces.IErebusSiteTreeProxy;

	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.patterns.command.SimpleCommand;

	/**
	 * @author grantheath
	 */
	public class ErebusInitializeProxiesCommand extends SimpleCommand implements ICommand 
	{
		override public function execute(notification : INotification) : void
		{
			var classReferenceProxy : IErebusClassReferenceProxy = facade.retrieveProxy(ErebusProxyNames.CLASS_REFERENCE_PROXY) as IErebusClassReferenceProxy;
			
			if(classReferenceProxy){
				var preferencesProxy : IErebusPreferencesProxy = facade.retrieveProxy(ErebusProxyNames.PREFERENCES_PROXY) as IErebusPreferencesProxy;
			
				if(preferencesProxy){
					var pageLoaderProxy : IErebusPageLoaderProxy = facade.retrieveProxy(ErebusProxyNames.PAGE_LOADER_PROXY) as IErebusPageLoaderProxy;
					
					if(pageLoaderProxy){
						var assetLoaderClassReferenceProxy : IErebusAssetLoaderClassReferenceProxy = facade.retrieveProxy(ErebusProxyNames.ASSET_LOADER_CLASS_REFERENCE_PROXY) as IErebusAssetLoaderClassReferenceProxy;
						
						if(assetLoaderClassReferenceProxy){
							var assetLoaderProxy : IErebusAssetLoaderProxy = facade.retrieveProxy(ErebusProxyNames.ASSET_LOADER_PROXY) as IErebusAssetLoaderProxy;
							
							if(assetLoaderProxy){
								var layersProxy : IErebusLayersProxy = facade.retrieveProxy(ErebusProxyNames.LAYERS_PROXY) as IErebusLayersProxy;
								
								if(layersProxy){
									var siteTreeProxy : IErebusSiteTreeProxy = facade.retrieveProxy(ErebusProxyNames.SITE_TREE_PROXY) as IErebusSiteTreeProxy;
									
									if(siteTreeProxy){
										sendNotification(ErebusInitializationNotifications.INITIALIZE_TRANSITION_MANAGERS);
										sendNotification(ErebusInitializationNotifications.INITIALIZE_URL_HANDELING);
									} else {
										var siteTreeProxyClass : Class = classReferenceProxy.siteTreeProxyClass;
										
										facade.registerProxy(new siteTreeProxyClass());
									}
								} else {
									var layersProxyClass : Class = classReferenceProxy.layersProxyClass;
									
									facade.registerProxy(new layersProxyClass());
								}
							} else {
								var assetLoaderProxyClass : Class = classReferenceProxy.assetLoaderProxyClass;
								
								facade.registerProxy(new assetLoaderProxyClass());
							}
						} else {
							var assetLoaderClassReferenceProxyClass : Class = classReferenceProxy.assetLoaderClassReferenceProxy;
							
							facade.registerProxy(new assetLoaderClassReferenceProxyClass());
						}
					} else {
						var pageLoaderProxyClass : Class = classReferenceProxy.pageLoaderProxyClass;
						
						facade.registerProxy(new pageLoaderProxyClass());
					}
				} else {
					var preferencesProxyClass : Class = classReferenceProxy.preferencesProxyClass;
					
					facade.registerProxy(new preferencesProxyClass());
				}
			} else {
				//TODO better undefined site tree proxy error
				throw new Error("site tree proxy undefined");
			}
		}
	}
}
